import { fetchHistoryAll } from "@/api/table.js";
import dayjs from "dayjs";

const history = {
  namespaced: true,
  state: {
    historyModulePage: {},
    formDataValue: {
      account: "",
      fileName: "",
      timer: "",
      model: "",
    },
    total: 0,
    current: 1,
    pageSize: 20,
    isLoading: false,
    tableData: [],
    modelList: [],
    modelNameDetailPath: {}
  },

  mutations: {
    SET_MODULE_PAGE: (state, page) => {
      state.historyModulePage[page.name] = page.number
    },
    SET_PAGE: (state, currentPage) => {
      state.current = currentPage
    },
    RESET_FORM: (state) => {
      state.formDataValue = {
        account: "",
        fileName: "",
        timer: "",
        model: "",
      } 
    },
    SAVE_FORM: (state, formData) => {
      state.formDataValue = formData
    },
    SET_ALL_DATA: (state, res) => {
      state.tableData = res.data.personnelList.map((item) => {
        return {
          ...item,
          isEdit: false,
          editValue: "",
          uploadTime: dayjs(item.uploadTime).format("YYYY-MM-DD HH:mm:ss"),
        };
      });
      if(res.status !== 200) {
        return this.$message.error(res.msg)
      }
      state.total = res.data.recCount;
      state.modelList = res.data.model
      state.modelNameDetailPath = res.data.model_name
    }
  },

  actions: {
    async getHistoryList({ commit, state }, data) {
      state.isLoading = true;
      try {
        const res = await fetchHistoryAll(data);
        commit('SET_ALL_DATA', res)
      } catch (err) {
        return err;
      } finally {
        state.isLoading = false;
      }
    },
  }
}

export default history